home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Misc / time.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  5KB  |  219 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* time.c - standard C time functions
  18.  */
  19.  
  20. #include <libraries/dos.h>
  21. #include <time.h>
  22. #include <math.h>
  23.  
  24. struct _MonthTable {
  25.     char *name;
  26.     int   days;
  27.     } _MonthTable[12] = {
  28.         { "Jan", 31 }, { "Feb", 28 }, { "Mar", 31 }, { "Apr", 30 },
  29.         { "May", 31 }, { "Jun", 30 }, { "Jul", 31 }, { "Aug", 31 },
  30.         { "Sep", 30 }, { "Oct", 31 }, { "Nov", 30 }, { "Dec", 31 }
  31.         };
  32.  
  33. char *(_DayTable[7]) = {
  34.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  35.     };
  36.  
  37. struct DateStamp StartStamp = { 0, 0, 0 };
  38.  
  39. char *ctime(timptr)
  40. time_t *timptr;
  41. {
  42.     return(asctime(localtime(timptr)));
  43. }
  44.  
  45. char *asctime(ts)
  46. struct tm *ts;
  47. {
  48.     char *buffer;
  49.  
  50.     buffer = malloc(64);   /* 64 is ample space to hold a date string */
  51.     if (!buffer)
  52.         return(0L);
  53.  
  54.     sprintf(buffer, "%s %s %d %d:%02d:%02d %d\n",
  55.            _DayTable[ts->tm_wday], _MonthTable[ts->tm_mon].name,
  56.            ts->tm_mday, ts->tm_hour, ts->tm_min,
  57.            ts->tm_sec, ts->tm_year+1900 );
  58.  
  59.     return(buffer);
  60. }
  61.  
  62. time_t time(tptr)
  63. time_t *tptr;
  64. {
  65.     struct DateStamp dos_date;
  66.     long  secs;
  67.  
  68.     DateStamp(&dos_date);
  69.     secs = dos_date.ds_Days * (60 * 60 * 24);
  70.     secs += dos_date.ds_Minute * 60;
  71.     secs += (dos_date.ds_Tick/CLK_TCK);
  72.  
  73.     if (tptr)
  74.         *tptr = (time_t) secs;
  75.  
  76.     return( (time_t) secs);
  77. }
  78.  
  79. double jday(t)
  80. time_t *t;
  81. {
  82.     /*  Julian date at epoch (12am Jan. 1, 1978) was 2,443,509.5 */
  83.     return ( ((double) (*t)) / (60.0 * 60.0 * 24.0) + 2443509.5);
  84. }
  85.  
  86. int dayofw(date)
  87. time_t *date;
  88. {
  89.     double dummy;
  90.  
  91.     return((int) rint(modf((jday(date)+1.0)/7.0, &dummy) * 7.0) % 7);
  92. }
  93.  
  94. struct tm *localtime(t)
  95. time_t *t;
  96. {
  97.     struct tm *tmval;
  98.     long secs,
  99.          days,
  100.          year;
  101.     int no_leap;
  102.     int month;
  103.     int hour;
  104.     int min;
  105.  
  106.     tmval = malloc(sizeof(struct tm));
  107.     if (!tmval)
  108.         return(NULL);
  109.  
  110.     secs = *t;
  111.     tmval->tm_wday = dayofw(t);
  112.  
  113.     days = secs / (60 * 60 * 24);
  114.     secs %= (60 * 60 * 24);
  115.  
  116.     year = 1978;
  117.     while (days > 365)    {
  118.         days -= ( year%4 ? 365 : 366 );
  119.         year++;
  120.         }
  121.  
  122. /* Special case: Enough days to make it through an additional non-leap year.
  123.  */
  124.     if ( (no_leap=(year%4)) && (days==365) )    {
  125.         year++;
  126.         no_leap = (year%4);
  127.         days = 0;
  128.         }
  129.  
  130.     tmval->tm_year = year - 1900;    /* Now know year and day of year */
  131.     tmval->tm_yday = days;
  132.  
  133.     if (no_leap)
  134.         _MonthTable[1].days = 28;
  135.     else
  136.         _MonthTable[1].days = 29;
  137.  
  138.     for (month = 0; month < 12; month++)    {
  139.         if (days < _MonthTable[month].days)
  140.             break;
  141.         days -= _MonthTable[month].days;
  142.         }
  143.  
  144.     tmval->tm_mon = month;        /* Now know month and day */
  145.     tmval->tm_mday = ++days;
  146.  
  147.     tmval->tm_hour = secs / (60 * 60);
  148.     secs %= (60 * 60);
  149.     tmval->tm_min = secs / 60;
  150.     tmval->tm_sec = secs % 60;
  151.  
  152.     tmval->tm_isdst = isdst(tmval);
  153.  
  154.     return(tmval);
  155. }
  156.  
  157. clock_t clock()
  158. {
  159.     struct DateStamp CurrStamp;
  160.     clock_t ticks;
  161.  
  162.     if (!StartStamp.ds_Days && !StartStamp.ds_Minute && !StartStamp.ds_Tick)
  163.         DateStamp(&StartStamp);
  164.  
  165.     DateStamp(&CurrStamp);
  166.  
  167. /* Beware of wraparound (e.g., when program has been running 497 days!-) */
  168.     ticks = (CurrStamp.ds_Days - StartStamp.ds_Days) * (86400 * CLK_TCK);
  169.     ticks += (CurrStamp.ds_Minute - StartStamp.ds_Minute) * (60 * CLK_TCK);
  170.     ticks += (CurrStamp.ds_Tick - StartStamp.ds_Tick);
  171.  
  172.     return(ticks);
  173. }
  174.  
  175. /* Timezones and Daylight Savings Time can really be a pain.  Perhaps an
  176.  * Amiga port of zoneinfo will show up in a future PDC release!
  177.  */
  178. int isdst(tmptr)
  179. struct tm *tmptr;
  180. {
  181.     return 0;
  182. }
  183.  
  184. struct tm *gmtime(t)
  185. time_t *t;
  186. {
  187.     return 0;
  188. }
  189.  
  190. /* mktime() returns -1 for illegal input.  Arguments prior to the epoch are 
  191.  * illegal.
  192.  */
  193. time_t mktime(tmptr)
  194. struct tm *tmptr;
  195. {
  196.     time_t retval = (time_t) -1;
  197.     long year, secs;
  198.  
  199.     if (tmptr != 0L && (year = tmptr->tm_year+1900) >= 1978) {
  200.         secs = tmptr->tm_sec;
  201.         secs += tmptr->tm_min * 60;
  202.         secs += tmptr->tm_hour * 60 * 60;
  203.         secs += tmptr->tm_yday * 60 * 60 * 24;
  204.         while (year-- > 1978) {
  205.             if (year % 4)
  206.                 secs += (60 * 60 * 24 * 365);
  207.             else
  208.                 secs += (60 * 60 * 24 * 366);
  209.             }
  210.         retval = (time_t) secs;
  211.         }
  212. }
  213.  
  214. double difftime(t1, t0)
  215. time_t t1, t0;
  216. {
  217.     return ((double) t1) - (double) t0;
  218. }
  219.